package oaSystem.web.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import oaSystem.biz.ActivityScheduleBiz;
import oaSystem.biz.AnnouncementBiz;
import oaSystem.biz.EmployeeBiz;
import oaSystem.biz.MessageBiz;
import oaSystem.entity.ActivitySchedule;
import oaSystem.entity.Announcement;
import oaSystem.entity.Employee;
import oaSystem.entity.Message;
import oaSystem.web.security.Authorize;

@Controller
public class HomeController {
	@Autowired
	private MessageBiz messageBiz;
	@Autowired
	private AnnouncementBiz announcementBiz;
	@Autowired
	private EmployeeBiz employeeBiz;
	@Autowired
	private ActivityScheduleBiz activityScheduleBiz;
	@Authorize
	@RequestMapping("/index")
	public String index(HttpSession session,Map<String, Object> model){
		Employee employee = (Employee)session.getAttribute("user");
		Integer userId=null;
		if(employee!=null){
			userId = employee.getId();//获得sessionId
		}
		//获取邮件信息
		List<Message> rmessageList = messageBiz.getMessageBySearch(userId, 1, 1, 10, null);
		List<Message> cmessageList = messageBiz.getMessageBySearch(userId, 2, 1, 10, null);
		model.put("rmessageList", rmessageList);
		model.put("cmessageList", cmessageList);
		
		List<Announcement> announcementList = announcementBiz.findAllAnnouncement();
		for (Announcement announcement : announcementList) {
			Employee employeeById = employeeBiz.fetchEmployeeById(announcement.getCreator().getId());
			announcement.setCreator(employeeById);
		}
		
		List<ActivitySchedule> scheduleList = activityScheduleBiz.findEventAfterToday(userId);
		model.put("announcementList", announcementList);
		model.put("scheduleList", scheduleList);
		return "index";
	}
	
	@RequestMapping("/error")
	public String error(){
		return "error";
	}
	
}
